package com.project.collectAPI.entity;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 店铺对象
 * */

@Data
public class StoreItem {

    public static String stroeName;
    public static Double ratio=0.0; //溢价率
    public static String[] filter; //过滤类

    public static volatile List<String> mainClass;   //"饿了么"
    public static volatile List<String> childClass;  //"饿了么:大润发(三水店)"、"饿了么:大润发(赤坎点)"
    private static List<Map<String,StoreJsonConfig>> configList; //当前运行店铺的配置，在运行时唯一


    static {
        mainClass=new ArrayList<>();
        childClass=new ArrayList<>();
        configList=new ArrayList<>();
    }


    //添加主类
    public static void addMainClass(String mainName){
        if(!mainClass.contains(mainName))
        mainClass.add(mainName);
    }
    public static List<String> getMainClass(){
        return mainClass;
    }
    public static void removeMainClass(String mainKey){
        mainClass.remove(mainKey);
    }


    //添加子类菜单
    public static void addChilds(String mainNameKey, List<String> childName){
        childName.forEach(item->{
            childClass.add(mainNameKey+"$"+item);
        });

    }

    //根据主菜单获取子菜单
    //推荐$PCJ2022111762006=夜宵解馋时刻
    public static List<String> getChildsFromKey(String keyName){
        List<String> res=new ArrayList<>();
        for (int i = 0; i < StoreItem.childClass.size(); i++) {
            String[] tempItem=StoreItem.childClass.get(i).toString().split("\\$");
            if(tempItem[0].equals(keyName)) {
                String[] tempItemSecond = tempItem[1].split("=");
                res.add(tempItemSecond[1]);  //[recommend_sub_1=热卖推荐]
            }
        }
        return res;
    }



    /**
     * 获取子元素集合
     * @param List<String> 类型
     * */
    public static List<String> getChilds(){
        return childClass;
    }
    /**
     * 获取最后一个元素
     * @param  返回String
     * */
    public static String getLastChild(){
        String lastElement = childClass.stream().reduce((a, b) -> b).orElse(null); //母婴宠物$319311057=宠物用品
        String first=lastElement.split("\\$")[0];
        String second=lastElement.split("\\$")[1].split("=")[1];
        return first+":"+second;
    }


    public static void removeChild(String mainKey,String childKey){
        for (int i = 0; i < childClass.size(); i++) {
            String key=childClass.get(i).split(":")[0];
            String val = childClass.get(i).split(":")[1];
            if(key.equals(mainKey) && val.equals(childKey)){
                childClass.remove(i);
            }
        }
       /* AtomicInteger index=new AtomicInteger(0);
        childClass.forEach(item->{
            //System.out.println(item);
            String key=item.split(":")[0];
            String val = item.split(":")[1];
            //System.out.println(key.equals(mainKey)+","+val.equals(childKey));
            if(key.equals(mainKey) && val.equals(childKey)){
                //childClass.remove(index.get());
                //System.out.println("移除:"+index.get());
                childClass.remove(index.get());
            }
            index.getAndIncrement();
        });*/
        //System.out.println("移除后:"+childClass.get(2));
    }

    /**
     * 添加配置
     * @param config 当前店铺配置
     * */
    public static void addConfig(Map<String,StoreJsonConfig> config){
        configList.add(config);
    }

    /**
     * 获取单个店铺配置
     * @param stroreName 店铺名称
     * StoreJsonConfig storeConfig=StoreItem.getConfig("大润发(赤坎)");
     * System.out.println(storeConfig);
     * */
    public static StoreJsonConfig getConfig(String stroreName){
        StoreJsonConfig config;
        for (int i = 0; i < configList.size(); i++) {
             config=configList.get(i).get(stroreName);
             if(!Objects.isNull(config)){
                 return config;
             }
        }
       return null;
    }

    /**
     * 获取所有店铺配置
     * @param stroreName 店铺名称
     * */
    public static List<Map<String,StoreJsonConfig>> getAllConfig(){
        return configList;
    }






} //Class End
